
package DAO;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DaoBaseMySql
{

    private String serverHost="localhost";
    private int puerto=3306;
    private String usuario="root";
    private String pass="";

    private boolean modoTrans;
    protected long transId;
    private Connection connection;

    public boolean ismModoTrans()
    {
        return modoTrans;
    }

    public long getmTransId()
    {
        return transId;
    }

    public DaoBaseMySql()
    {
        modoTrans = false;
        transId = 0l;

    }

    public DaoBaseMySql(long pTransId)
    {
        modoTrans = true;
        transId = pTransId;

    }

    public long beginTransaction()
    {
        Connection wCnn = getConnection();
        return beginTransaction(wCnn);

    }

    public long beginTransaction(Connection wCnn)
    {
        try
        {
            transId = System.currentTimeMillis();
            wCnn.setAutoCommit(false);
            TransactionStorage.addTransaction(transId, wCnn);
            modoTrans = true;
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DaoBaseMySql.class.getName()).log(Level.SEVERE, null, ex);
        }
        return transId;

    }

    public void setTransaction(long pTransId)
    {
        modoTrans = true;
        transId = pTransId;

    }

    public void commitTransaction(long pTransId)
    {
        Connection wTx = TransactionStorage.getTransaction(pTransId);
        if (wTx != null)
        {
            try
            {
                wTx.commit();
                wTx.close();
                TransactionStorage.removeTransaction(pTransId);
            }
            catch (Exception ex)
            {
                //TODO tratar la exception
            }
        }

    }

    public void rollbackTransaction(long pTransId)
    {
        Connection wTx = TransactionStorage.getTransaction(pTransId);
        if (wTx != null)
        {
            try
            {
                wTx.rollback();
                wTx.close();
                TransactionStorage.removeTransaction(pTransId);
            }
            catch (Exception ex)
            {
                //TODO tratar la exception
            }
        }

    }

    protected void closeConnection()
    {
        try
        {
            if (!modoTrans && !connection.isClosed())
            {
                connection.close();
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DaoBaseMySql.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    protected Connection getConnection()
    {
        Connection wConn;
        if (modoTrans)
        {
            wConn = TransactionStorage.getTransaction(transId);
            connection = wConn;
        }
        else
        {
            if (connection == null)
            {
                MysqlDataSource wDS = new MysqlDataSource();
                wDS.setServerName(this.serverHost);
                wDS.setPort(puerto);
                wDS.setDatabaseName("torneofutbol");
                wDS.setUser(this.usuario);
                wDS.setPassword(this.pass);
                try
                {
                    connection = wDS.getConnection();
                }
                catch (SQLException ex)
                {
                    Logger.getLogger(EquipoDAO.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else
            {
                try
                {
                    if (connection.isClosed())
                    {
                        connection = null;
                        connection = getConnection();
                    }
                }
                catch (SQLException ex)
                {
                    Logger.getLogger(DaoBaseMySql.class.getName()).log(Level.SEVERE, null, ex);
                }
            }


        }
        return connection;

    }
}
